import sys

def get_sending_data_from_client(filename):
    seq_list = []
    time_list = []
    with open(filename) as f:
        for line in f.readlines():
            fields = line.split(',')
            # SENDING DATA senderid=1687940413, seq=10, send_time=1687940413068780201, amount=10
            if 'SENDING DATA' in fields[0]:
                seq = fields[1].split("=")[1]
                send_time = int(fields[2].split("=")[1])/1.0e6
                seq_list.append(seq)
                time_list.append(send_time)

    return time_list, seq_list


def get_recving_data_from_server(filename):
    recv_seq_list = []
    with open(filename) as f:
        for line in f.readlines():
            fields = line.split(',')
            # INCOMING DATA RECEIVED senderid=1687940413, seq=1, send_time=1687940413068583407, recv_time=1687940413056637493, 1delay=-0.0119
            if 'INCOMING DATA' in fields[0]:
                seq = fields[1].split("=")[1]
                recv_seq_list.append(seq)
    
    return recv_seq_list


def generate_trace_pps(time_list, send_seq_list, recv_seq_list):
    init_time = time_list[0]
    # pps_list = []
    output = 'wireless.pps'
    with open(output, 'w') as f:
        for i, seq in enumerate(send_seq_list):
            pps = time_list[i] - init_time
            if seq in recv_seq_list:
                # pps_list.append(pps)
                f.write(str(pps) + '\n')
            else:
                # pps_list.append(-pps)
                f.write('-' + str(pps) + '\n')

    # return pps_list

if __name__ == '__main__':
    time_list, seq_list = get_sending_data_from_client(sys.argv[1])
    recv_seq_list = get_recving_data_from_server(sys.argv[2])
    send_len = len(seq_list)
    recv_len = len(recv_seq_list)
    print('send cnt', send_len)
    print('recv cnt', recv_len)
    print('loss rate:', (send_len - recv_len) / send_len)

    # generate_trace_pps(time_list, seq_list, recv_seq_list)
